<?php
/**
 * --------------------------------
 * 路由类
 * --------------------------------
 * 1. 当为网页服务时 解析 URL
 * 2. 调用相应控制器与方法
 * 3. 支持 CLI 模式
 * --------------------------------
 */
namespace phpless\core;

class Route
{
    protected static $instance;
    public $module;
    public $ctrl;
    public $action;
    private function __construct()
    {
        // dump('Route OK !!!');
        // 判断 URL_MODEL
        $url_model=config('URL_MODEL');
        $is_module = config('IS_MODULE');
        $default_module=$is_module?config('DEFAULT_MODULE'):'';
        $default_ctrl=config('DEFAULT_CTRL');
        $default_action=config('DEFAULT_ACTION');
        switch($url_model){
            case 0:
                if(PHP_SAPI === 'cli'){
                    $_GET['a'] = APP_ARGV[2] ?? '';
                    $_GET['c'] = APP_ARGV[1] ?? '';
                }
                $get=$_GET;
                $ctrl=isset($get['c'])?$get['c']:$default_ctrl;
                $action=isset($get['a'])?$get['a']:$default_action;
                $module_ctrl = explode('.',$ctrl);
                if($is_module && count($module_ctrl) >= 2){
                    $module=$module_ctrl[0];
                    $ctrl=$module_ctrl[1];
                }else{
                    $ctrl=str_replace('.','',$ctrl);
                    $module=$default_module;
                }
                $this->module=$module;
                $this->ctrl=$ctrl;
                $this->action=$action;
                break;
            case 1:
                if(PHP_SAPI === 'cli'){
                    $path_info = APP_ARGV[1] ?? '';
                }else{
                    $path_info=isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
                }
                $path_info=trim($path_info,'/');
                if(empty($path_info)){
                    $this->module=$default_module;
                    $this->ctrl=$default_ctrl;
                    $this->action=$default_action;
                }else{
                    $path_arr=explode('/',$path_info);

                    $module_ctrl = explode('.',$path_arr[0]);
                    if($is_module && count($module_ctrl) >= 2){
                        $this->module=$module_ctrl[0];
                        $this->ctrl=$module_ctrl[1];
                    }else{
                        $this->ctrl=str_replace('.','',$path_arr[0]);
                        $this->module=$default_module;
                    }

                    if(!isset($path_arr[1])){
                        $this->action=$default_action;
                    }else{
                        $this->action=$path_arr[1];
                    }
                    unset($path_arr[0]);
                    unset($path_arr[1]);
                    // 转化为URL参数
                    $count=count($path_arr);
                    for($i=2;$i<=$count;$i=$i+2){
                        $_GET[$path_arr[$i]]=$path_arr[$i+1];
                    }
                }
                break;
            default:
                throw new \Exception("请配置 URL_MODEL");
                break;
        }
    }
    // 单例模式获取Route类
    static function getInstance()
    {
        if(self::$instance){
            return self::$instance;
        }else{
            self::$instance=new Route();
            return self::$instance;
        }
    }
}
